package com.atguigu.day03;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Flink11_Transform_RePartition {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());

        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        env.setParallelism(4);

        //streamSource.map(r->r)
        SingleOutputStreamOperator<String> map = streamSource.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                return value;
            }
        }).setParallelism(2);

        //map.keyBy(r-r)
        KeyedStream<String, String> keyedStream = map.keyBy(new KeySelector<String, String>() {
            @Override
            public String getKey(String value) throws Exception {
                return value;
            }
        });

        //shuffle
        DataStream<String> shuffle = map.shuffle();
        //rebalance
        DataStream<String> rebalance = map.rebalance();
        //重缩放
        DataStream<String> rescale = map.rescale();
        //广播
        DataStream<String> broadcast = map.broadcast();
        //全局
        DataStream<String> global = map.global();
        //forward
        DataStream<String> forward = map.forward();


        map.print("原始数据:").setParallelism(2);
        keyedStream.print("keyby:");
        shuffle.print("shuffle:");
        rebalance.print("rebalance");
        rescale.print("rescale:");
        broadcast.print("broadcast:");
        global.print("global:");
        forward.print("forward:").setParallelism(2);

        env.execute();

    }
}
